//
//  NSFileManager+Extension.m
//  WHPersonalData
//
//  Created by mac on 2018/8/8.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "NSFileManager+Extension.h"
#import <AVFoundation/AVFoundation.h>

@implementation NSFileManager (Extension)

/*
 1.方法名: 创建文件夹
 @param  path    目标文件夹
 @return BOOL    是否创建成功
 */
+(BOOL)createFileDirectoryAtPath:(NSString*)path
{
    NSFileManager * fileManager = [NSFileManager defaultManager];
    NSError * error = nil;
    //当前路径指的是可执行程序的路径而不是代码的路径
    //创建目录  返回值:创建成功返回YES 失败返回NO
    BOOL isCreateSuccess = [fileManager createDirectoryAtPath:path
                            //创建目录的路径
                                  withIntermediateDirectories:YES//路径不存在是否创建目录 YES代表创建(会把不存在的目录也创建) NO代表不创建
                                                   attributes:nil    //文件属性(权限),通常写nil代表默认权限
                                                        error:&error];
    if(isCreateSuccess)
    {
        NSLog(@"success!");
    }
    else
    {
        NSLog(@"error = %@",error);
    }
    return isCreateSuccess;
}

/**
 2.方法名:拷贝指定文件夹里的内容到指定目录下
 @param sourcePath 源文件夹
 @param toPath 目标文件
 @return 是否完成拷贝
 */
+(BOOL)copyFileFromPath:(NSString *)sourcePath toPath:(NSString *)toPath
{
    BOOL copySucceed = YES;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:toPath]) {
        NSError *error = nil;
        [fileManager createDirectoryAtPath:toPath withIntermediateDirectories:YES attributes:nil error:&error];
        if (error) {
            //TODO 创建指定拷贝目录时候失败
        }
    }

    NSArray* array = [fileManager contentsOfDirectoryAtPath:sourcePath error:nil];
    
    for(int i = 0; i<[array count]; i++)
    {
        
        NSString *fullPath = [sourcePath stringByAppendingPathComponent:[array objectAtIndex:i]];
        NSString *fullToPath = [toPath stringByAppendingPathComponent:[array objectAtIndex:i]];
        //判断是不是文件夹
        BOOL isFolder = NO;
        //判断是不是存在路径 并且是不是文件夹
        BOOL isExist = [fileManager fileExistsAtPath:fullPath isDirectory:&isFolder];
        
        if (isExist)
        {
            NSError *err = nil;
            [[NSFileManager defaultManager] copyItemAtPath:fullPath toPath:fullToPath error:&err];
            if (err) {
                copySucceed=NO;
            }
            if (isFolder)
            {
                [self copyFileFromPath:fullPath toPath:fullToPath];
                
            }
        }
    }
    return copySucceed;
}

/*
 3.方法名:计算文件的大小（字节)
 @param filePath   目标文件路径
 @return long long 返回长整
 */

+(long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}

/*
 4.方法名:删除文件夹下的所有文件
 @param filePath 目标文件夹
 @return void    返回空
 */

+(void)deleteFileDirectoryAtPath:(NSString*)fileDirectoryPath
{
    //删除
    NSString *DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:fileDirectoryPath];
    NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:DocumentsPath];
    for (NSString *fileName in enumerator) {
        [[NSFileManager defaultManager] removeItemAtPath:[DocumentsPath stringByAppendingPathComponent:fileName] error:nil];
    }
}

/*
 5.方法名：删除指定路径文件
 @param filePath 目标文件路径
 @return void    返回空
 */

+(void)deleteFileWithFilePath:(NSString*)filePath
{
     NSString *docPath = documentsPath;
     NSFileManager *fileMgr = [NSFileManager defaultManager];
    BOOL isEqualAtPath =[fileMgr contentsEqualAtPath:docPath andPath:filePath];
    if (isEqualAtPath==NO)
    {
        BOOL bRet = [fileMgr fileExistsAtPath:filePath];
        if (bRet)
           {
            NSError *err;
            BOOL  isSuccess = [fileMgr removeItemAtPath:filePath error:&err];
               
               if (isSuccess ==YES)
               {
                   DebugLog(@"删除文件成功");
               }else
               {
                   DebugLog(@"删除文件失败");
               }
           }
    }
}

/*
 6.方法名：移动文件
 @param sourceUrl 原文件路径
 @param sourceUrl 目标文件路径
 @return 如果成功返回NSString 如果失败返回NSError
 */

+(void)movChangeMP4WithAVURLAsset:(NSURL*)sourceUrl witSavePath:(NSString*)savePath succesefulBlock:(void (^)(NSString*  responseObject))succese andFailureBlock:(void (^)(NSError *error))failure
{
    AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:sourceUrl options:nil];
    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
    if ([compatiblePresets containsObject:AVAssetExportPresetHighestQuality]) {
        
        AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetMediumQuality];
        exportSession.outputURL = [NSURL fileURLWithPath:savePath];
        exportSession.outputFileType = AVFileTypeMPEG4;
        exportSession.shouldOptimizeForNetworkUse = YES;
        [exportSession exportAsynchronouslyWithCompletionHandler:^(void)
         {
            
             switch (exportSession.status) {
                
                 case AVAssetExportSessionStatusUnknown:
                     NSLog(@"AVAssetExportSessionStatusUnknown");
                     break;
                 case AVAssetExportSessionStatusWaiting:
                     NSLog(@"AVAssetExportSessionStatusWaiting");
                     break;
                 case AVAssetExportSessionStatusExporting:
                     NSLog(@"AVAssetExportSessionStatusExporting");
                     break;
                 case AVAssetExportSessionStatusCompleted:
                     NSLog(@"AVAssetExportSessionStatusCompleted");
                     succese(@"succese");
                     break;
                 case AVAssetExportSessionStatusFailed:
                     NSLog(@"AVAssetExportSessionStatusFailed");
                     break;
             }
         }];
    }
}

@end
